<?Lassoscript
// Last modified 8/7/11 by ECL, Landmann InterActive

// FUNCTIONALITY
// Include for Slider Entry add response page

// CHANGE NOTES
// 8/7/11
// First implementation

// Debugging
// Var('svDebug' = 'Y');

// Converting action_params
Var('vError' = '');
Var('vOption' = '');
// The action_param "Action" is passed by the frm_sliderentry.inc form to indicate whether it is an add or edit
// Action = Update, means the request was an Edit (from frm_sliderentry.inc)
// Action = Add, means the request was an Add (from frm_sliderentry.inc)
Var('vAction' = Action_Param('Action'));

If($svDebug == 'Y');
	'<br>\n';
	'<b>sliderentry_addresponse.inc</b><br>\n';
/If;

// Checking referrer to make sure it's only posting from the Add page
// ============= ADD ========
If($vAction == 'Add');

	// Convert action_params
	Include(($svAdminHelpersPath)+'sliderentry_params.inc');

	// If an error was generated from the Params, no point continuing, so bail immediately
	If($vError == '');

		// Construct the query to add the record
		Var('SQLQueryAddSliderEntry' = (StripBackticks('/* SQLQueryAddSliderEntry */
		INSERT INTO '+($svSiteDatabase)+'.'+($svSliderTable)+
			' SET
				Slider_Title="'+(Encode_SQL($vSlider_Title))+'",
				Slider_Caption="'+(Encode_SQL($vSlider_Caption))+'",
				Slider_URL="'+(Encode_SQL($vSlider_URL))+'",
				Slider_Thumb="'+(Encode_SQL($vSlider_Thumb))+'",
				Active="'+(Encode_SQL($vActive))+'",
				DateModified="'+(Date_Format(Date_GetCurrentDate,-DateFormat='%Q'))+'";
				SELECT LAST_INSERT_ID() AS NewSliderEntryID;')));
			
			If($svDebug == 'Y');
				('128: SQLQueryAddSliderEntry = '+($SQLQueryAddSliderEntry)+'<br>\n');
			/If;
		
			Inline($IV_Sliders, -Table=$svSliderTable, -SQL=$SQLQueryAddSliderEntry);
				
				If((Error_CurrentError)==(Error_NoError));
			
					// If no error, Add was successful, dump out Error 1001 "Add Successful"
					// Copying NewSliderEntryID to vNewID to use in links
					$vError = '1001';
					$vOption = Var('vHeadline');
					Var('vNewID' = Field('NewSliderEntryID'));
					If($svDebug == 'Y');
						('137: vError = '+($vError)+'<br>\n');
					/If;
			
				// There was an error
				Else;
					$vError = Error_CurrentError;
				/If;
			
			/Inline;

	/If;

// ============= EDIT ========
// Action = Update, means the request came from the Edit page
Else($vAction == 'Update');

	// Convert action_params
	Include(($svAdminHelpersPath)+'sliderentry_params.inc');
	Var('vID' = '');
	Var('vID' = Action_Param('id'));

	// Copying the ID to vNewID to build link
	// This is so we can just use vNewID at bottom of page for both add and edit
	Var('vNewID' = Var('vID'));

	// If an error was generated from the Params, no point continuing, so bail immediately
	If($vError == '');

		// Construct the query to Update the record
		// Not changing the Date_Created fields
		Var('SQLUpdateSliderEntry' = (StripBackticks('/* SQLUpdateSliderEntry */
		UPDATE '+($svSliderTable)+' SET
			Slider_Title="'+(Encode_SQL($vSlider_Title))+'",
			Slider_Caption="'+(Encode_SQL($vSlider_Caption))+'",
			Slider_URL="'+(Encode_SQL($vSlider_URL))+'",
			Slider_Thumb="'+(Encode_SQL($vSlider_Thumb))+'",
			Active="'+(Encode_SQL($vActive))+'",
			DateModified="'+(Date_Format(Date_GetCurrentDate,-DateFormat='%Q'))+'"
			WHERE slider_id = "'+($vID)+'"')));

		If($svDebug == 'Y');
			('224: SQLUpdateSliderEntry = '+($SQLUpdateSliderEntry)+'<br>\n');
		/If;
	
			Inline($IV_Sliders, -Table=$svSliderTable, -SQL=$SQLUpdateSliderEntry);
			
			If((Error_CurrentError) == (Error_NoError));
		
				// If no error, Update was successful, dump out Error 1011 "Update Successful"
				$vError = '1011';
				$vOption = Var('vID');
				If($svDebug == 'Y');
					('233: vError = '+($vError)+'<br>\n');
				/If;
		
			// There was an error
			Else;
				$vError = Error_CurrentError;
			/If;
		
		/Inline;

	// vError == ''
	/If;

// ============= DELETE ========
Else($vAction == 'Delete');

	// Convert action_params
	Var('vID' = Action_Param('ID'));
		
	// Build the Delete Query
	Var('SQLDeleteQuery' = string);
	Var('SQLDeleteQuery' = (StripBackticks('DELETE FROM '+($svSiteDatabase)+'.'+($svSliderTable)+' WHERE slider_id = "'+($vID)+'"')));

	// Delete the record
	Inline($IV_Sliders, -SQL=$SQLDeleteQuery);
		// If delete successful, output 1021 "Deletion Successful" and go ahead and delete attachments,
		If((Error_Code) == '0');
			$vError = '1021';
			$vOption = $vID;
		// If delete failed, output error 1022, "Delete Failed"
		Else;
			$vError = '1022';
			$vOption = Error_CurrentError;
			LI_URLRedirect(-Page=(($svAdminControllersPath)+($svAddResponsePage)),-ExParams=('DataType='+($vDataType)),-UseError='Y',-Error=$vError,-Option=$vOption);
		/If;
	/Inline;

		
// Action is wrong, somebody is messing with us
// Dump out error 9002, "Database Error"
Else;
	$vError = '9002';
/If;


// Deal with the results of the processing

// ============= ADD  ========
// If any other error other than 1001 "Add Successful" do a redirect
If($vAction == 'Add');
	// Standard Error Table
	If($vError == '1001');
		LI_ShowError3(-ErrNum=$vError, -Option=$vOption);

// Edit New Listing Link -->
?>
<div align="left" style="margin-bottom:20px;">
	View the record for <a href="[$svAdminControllersPath][$svEditRecordPage]?Datatype=SliderEntry&ID=[Var('vNewID')]&New=Y"><b>Record [$vNewID]</b></a>
</div>
<?Lassoscript
	Else;
		LI_URLRedirect(-Page=(($svAdminControllersPath)+($svAddPage)),-ExParams=('DataType='+($vDataType)),-UseError='Y',-Error=$vError,-Option=$vOption,-UseArgs='Y');
	/If;
/If;


// ============= EDIT ========
// If vAction = Edit, Always redirect to edit page
// If record updated OK, will get a 1011 "Update Successful" message, but do NOT pass the Params, only the ID
// This forces the system to do a new lookup
If($vAction == 'Update');
	If($vError == '1011');
		LI_URLRedirect(-Page=(($svAdminControllersPath)+($svEditRecordPage)),-ExParams=('DataType='+($vDataType)+'&ID='+($vNewID)+'&New=Y'),-UseError='Y',-Error=$vError,-Option=$vOption,-UseArgs='N');
	Else;
		LI_URLRedirect(-Page=(($svAdminControllersPath)+($svEditRecordPage)),-ExParams=('DataType='+($vDataType)+'&New=Y'),-UseError='Y',-Error=$vError,-Option=$vOption,-UseArgs='Y');
	/If;
/If;

// ============= DELETE ========
// If Action = Delete, redirect to search page
If($vAction == 'Delete');
	LI_URLRedirect(-Page=(($svAdminControllersPath)+($svSearchPage)),-ExParams=('DataType='+($vDataType)),-UseError='Y',-Error=$vError,-Option=$vOption,-UseArgs='N');
/If;
?>
